package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MultiPaneWidthOri extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multipanewidth);
	}
	
	public static class PlanetListFragment extends ListFragment{
		int mLastIndex;
		boolean mMultiPane;//true 태블릿
		
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			setListAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, PlanetInfo.PLANET));
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			
			if (savedInstanceState != null){
				mLastIndex = savedInstanceState.getInt("lastindex");
			}
			
			View descPanel = getActivity().findViewById(R.id.descpanel);
			if (descPanel != null && descPanel.getVisibility() == View.VISIBLE){
				//태블릿
				mMultiPane = true;
				onListItemClick(getListView(), null, mLastIndex, 0);
			}
		}//end of method onActivityCreated
		
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("lastindex", mLastIndex);
		}
		
		public void onListItemClick(ListView l, View v, int position, long id) {
			//super.onListItemClick(l, v, position, id);
			mLastIndex = position;
			getListView().setItemChecked(position, true);
			
			if (mMultiPane){
				//태블릿
				FragmentManager fm = getFragmentManager();
				DescFragment df = (DescFragment) fm.findFragmentById(R.id.descpanel);
				if (df == null || df.mNowIndex != position){
					df = DescFragment.newInstance(position);
					fm.beginTransaction().replace(R.id.descpanel, df).commit();
				}
			}else{
				//핸드셋
				Intent intent = new Intent(getActivity(), DescActivity.class);
				intent.putExtra("index", position);
				startActivity(intent);
			}
		}//end of method onListItemClick
		
		
	}//end of class PlanetListFragment
	
	public static class DescFragment extends Fragment{
		int mNowIndex;
		
		public static DescFragment newInstance(int index) {
			DescFragment df = new DescFragment();
			df.mNowIndex = index;
			return df;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View root = inflater.inflate(R.layout.descfragment, container, false);
			TextView tv = (TextView) root.findViewById(R.id.txtdescription);
			tv.setText(PlanetInfo.DESC[mNowIndex]);
			return root;
		}
		
	}//end of class DescFragment
	
	public static class DescActivity extends Activity{
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
				finish();
				return;
			}
			int index = getIntent().getExtras().getInt("index");
			DescFragment details = DescFragment.newInstance(index);
			getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
		}
	}//end of class DescActivity
}






